<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use think\Request;

class System extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }
    public function addModulea()
    {
        $this->checklogin();
        $list = Db::table('nx_admin_modulea')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('list',$list);
        return $this->fetch('system/addModulea');
    }
    public function doAddModulea()
    {
        $level = $this->request->post('level');
        $route = $this->request->post('route');
        $title = $this->request->post('title');
        $pid = $this->request->post('pid');
        $icon = $this->request->post('icon');
        $modulea = $this->request->post('modulea');
        $sort = $this->request->post('sort');
        $data = [
            'level'=>$level,
            'route'=>$route,
            'title'=>$title,
            'pid'=>$pid,
            'icon'=>$icon,
            'modulea'=>$modulea,
            'sort'=>$sort,
            'createtime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_admin_modulea')->insert($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function editModulea()
    {
        $id = $this->request->param('id');
        $modulea = Db::table('nx_admin_modulea')->where('id',$id)->find();
        $list = Db::table('nx_admin_modulea')
            ->where([
                'delstatus'=>1,
                'level'=>1
            ])
            ->select();
        $this->assign('list',$list);
        $this->assign('modulea',$modulea);
        return $this->fetch('system/editModulea');
    }
    public function doEditModulea()
    {
        $id = $this->request->post('id');
        $level = $this->request->post('level');
        $route = $this->request->post('route');
        $title = $this->request->post('title');
        $pid = $this->request->post('pid');
        $icon = $this->request->post('icon');
        $modulea = $this->request->post('modulea');
        $sort = $this->request->post('sort');
        $data = [
            'level'=>$level,
            'route'=>$route,
            'title'=>$title,
            'pid'=>$pid,
            'icon'=>$icon,
            'modulea'=>$modulea,
            'sort'=>$sort,
            'updatetime'=>date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_admin_modulea')->where('id',$id)->update($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function moduleaList()
    {
        $this->checklogin();
        return $this->fetch('system/moduleaList');
    }
    public function getModuleaList()
    {
        $key = $this->request->post('keyword');
        $list = Db::table('nx_admin_modulea')
            ->where('title|modulea','like','%'.$key.'%')
            ->where('delstatus',1)
            ->order('id','asc')
            ->select();
        foreach ($list as $k=>$v){
            $modulea = Db::table('nx_admin_modulea')->where('id',$v['pid'])->field('title')->find();
            if($modulea){
                $list[$k]['ptitle']=$modulea['title'];
            }else{
                $list[$k]['ptitle']='';
            }
            if(!$v['icon']){
                $list[$k]['icon']='';
            }
        }
        return $this->api_resp_success('','',$list);
    }
    public function deletemodulea()
    {
        $id = $this->request->post('id');
        $res = Db::name('nx_admin_modulea')->where('id','in',$id)->update(['delstatus'=>4,'updatetime'=>date('Y-m-d H:i:s',time())]);
        return $this->api_resp_success('删除成功','',$res);
    }
    public function rule()
    {
        $this->checklogin();
        return $this->fetch('system/rule');
    }
    public function getRuleList()
    {
        $key = $this->request->post('keyword');
        $list = Db::table('nx_admin_rule')
            ->where('rulename','like','%'.$key.'%')
            ->where('delstatus','1')->select();
        foreach($list as $k=>$v){
            $names1 = Db::table('nx_admin_modulea')->where('id','in',$v['modulea_one'])->field('title')->select();
            $names2 = Db::table('nx_admin_modulea')->where('id','in',$v['modulea_two'])->field('title')->select();
            $str1 = '';
            $str2 = '';
            foreach ($names1 as $key=>$val){
                $str1 = $str1.','.$val['title'];
            }
            foreach ($names2 as $key=>$val){
                $str2 = $str2.','.$val['title'];
            }
            $str1 = ltrim($str1, ",");
            $str2 = ltrim($str2, ",");
            $list[$k]['modulea_one'] = $str1;
            $list[$k]['modulea_two'] = $str2;
        }
        return $this->api_resp_success('','',$list);
    }
    public function addRule()
    {
        $list = Db::table('nx_admin_modulea')
            ->where(['delstatus'=>1,'level'=>1])
            ->select();
        $list2 = Db::table('nx_admin_modulea')
            ->where(['delstatus'=>1,'level'=>2])
            ->select();
        $this->assign('list',$list);
        $this->assign('list2',$list2);
        return $this->fetch('system/addRule');
    }
    public function doAddule()
    {
        $dataarr = $this->request->param();
        $ids = implode(',',$dataarr['ids']);

        if($dataarr['ids2'] == ''){
            $ids2 = '';
        }else{
            $ids2 = implode(',',$dataarr['ids2']);
        }
        $data = [
            'rulename'=>$dataarr['title'],
            'modulea_one' => $ids,
            'modulea_two' => $ids2,
            'createtime' => date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_admin_rule')->insert($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    //修改权限
    public function editRule()
    {
        $id = $this->request->param('id');
        $res = Db::name('nx_admin_rule')->where('id',$id)->find();
        $list = Db::table('nx_admin_modulea')
            ->where(['delstatus'=>1,'level'=>1])
            ->select();
        $list2 = Db::table('nx_admin_modulea')
            ->where(['delstatus'=>1,'level'=>2])
            ->select();
        $modulea_one = explode(',',$res['modulea_one']);
        $modulea_two = explode(',',$res['modulea_two']);
        foreach ($list as $k=>$v){
            if(in_array($v['id'],$modulea_one)){
                $list[$k]['check'] = 1;
            }else{
                $list[$k]['check'] = 0;
            }
        }
        foreach ($list2 as $ke=>$va){
            if(in_array($va['id'],$modulea_two)){
                $list2[$ke]['check'] = 1;
            }else{
                $list2[$ke]['check'] = 0;
            }
        }
        $this->assign('mid',$id);
        $this->assign('data',$res);
        $this->assign('list',$list);
        $this->assign('list2',$list2);
        return $this->fetch('system/editRule');
    }
    public function doEditRule()
    {
        $dataarr = $this->request->param();
        $ids = implode(',',$dataarr['ids']);

        if($dataarr['ids2'] == ''){
            $ids2 = '';
        }else{
            $ids2 = implode(',',$dataarr['ids2']);
        }
        $data = [
            'rulename'=>$dataarr['title'],
            'modulea_one' => $ids,
            'modulea_two' => $ids2,
            'updatetime' => date('Y-m-d H:i:s',time())
        ];
        $res = Db::name('nx_admin_rule')->where('id',$dataarr['id'])->update($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function deleterule()
    {
        $id = $this->request->post('id');
        $res = Db::name('nx_admin_rule')->where('id','in',$id)->update(['delstatus'=>4,'updatetime'=>date('Y-m-d H:i:s',time())]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function role()
    {
        return $this->fetch('system/role');
    }
    public function getRoleList()
    {
        $list = Db::table('nx_admin_role')->where('delstatus',1)->select();
        foreach ($list as $k=>$v){
            $data = Db::table('nx_admin_rule')->where('id','in',$v['rule'])->field('rulename')->select();
            $str1 = '';
            foreach ($data as $key=>$val){
                $str1 = $str1.','.$val['rulename'];
            }
            $str1 = ltrim($str1, ",");
            $list[$k]['rule'] = $str1;
        }
        return $this->api_resp_success('','',$list);
    }
    public function addRole()
    {
        $list = Db::table('nx_admin_rule')->where('delstatus',1)->select();
        $this->assign('list',$list);
        return $this->fetch('system/addRole');
    }
    public function doAddrole()
    {
        $list = $this->request->param();
        $data = [
            'rolename'=>$list['title'],
            'rule' =>implode(',',$list['ids']),
            'createtime'=>date('Y-m-d H;i:s',time())
        ];
        $res = Db::name('nx_admin_role')->insert($data);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
    public function deleterole()
    {
        $id = $this->request->post('id');
        $res = Db::name('nx_admin_role')->where('id','in',$id)->update(['delstatus'=>4]);
        if($res){
            return $this->api_resp_success('','',$res);
        }
    }
}
